package com.jcfk.eam.controller.parent;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.core.exception.CustomException;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.eam.domain.query.base.EamAssetModelQuery;
import com.jcfk.eam.domain.dto.base.EamAssetModelDTO;
import com.jcfk.eam.domain.uploadExcel.base.EamAssetModelUploadExcel;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import com.jcfk.eam.service.base.EamAssetModelService;
import com.jcfk.common.core.utils.ExcelUtils;
import com.jcfk.eam.domain.excel.base.EamAssetModelExcel;
import java.util.List;
/**
 * 资产型号
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2024-07-24
 */
@Api(tags = {"资产型号管理"})
@RestController
@RequestMapping("eamassetmodel")
public class EamAssetModelController {
    @Autowired
    private EamAssetModelService eamAssetModelService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamAssetModelQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询资产型号")
    @PostMapping("/queryByPage")
    public RetPage<EamAssetModelDTO> queryByPage(@RequestBody ReqPageParam<EamAssetModelQuery> requestParam) {
        DataPage<EamAssetModelDTO> page = new DataPage<EamAssetModelDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamAssetModelDTO.class);
        page = eamAssetModelService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamAssetBrandQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取资产型号列表", notes = "获取资产型号列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamAssetModelDTO>> getList(@RequestBody EamAssetModelQuery query) {
        if(query.getDeleted() == null)
            query.setDeleted(0);
        if(query.getEnabled() == null)
            query.setEnabled(1);

//        List<EamAssetModelDTO> list = eamAssetModelService.getList(query);

        return RetResult.ok(eamAssetModelService.queryByPage(query));
    }

    /**
     *  资产型号
     * 根据id 查询详情
     */
    @ApiOperation("查询资产型号")
    @GetMapping("/get/{id}")
    public RetResult<EamAssetModelDTO> get(@PathVariable("id") String id) {
            EamAssetModelDTO item = eamAssetModelService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理资产型号
     */
    @ApiOperation("保存资产型号")
    @PostMapping("/save")
    @RequestLogHeader(modular = "资产型号管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamAssetModelDTO eamAssetModel) {
        if(1 == eamAssetModel.getEnabled()){
            EamAssetModelQuery query = new EamAssetModelQuery();
            query.setModelNo(eamAssetModel.getModelNo());
            query.setBrandId(eamAssetModel.getBrandId());
            query.setMakerId(eamAssetModel.getMakerId());
            query.setAssetKind(eamAssetModel.getAssetKind());
            query.setEnabled(1);
            query.setDeleted(0);
            List<EamAssetModelDTO> getList = eamAssetModelService.getList(query);

            if(getList != null && !getList.isEmpty()){
                for (EamAssetModelDTO set : getList){
                    if(!set.getId().equals(eamAssetModel.getId()))
                        throw new CustomException("相同类型、品牌下型号名称不能重复，保存失败");
                }
            }
        }

        if (StrUtil.isEmpty(eamAssetModel.getId())){
            // 新增
            eamAssetModelService.insert(eamAssetModel);
        } else{
            // 更新
            eamAssetModelService.update(eamAssetModel);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除资产型号
     */
    @ApiOperation("批量删除资产型号")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "资产型号管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamAssetModelDTO eamAssetModel) {
        return RetResult.ok(eamAssetModelService.logicDeleteList(eamAssetModel));
    }


    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody EamAssetModelQuery requestParam, HttpServletResponse response) throws Exception {
        List<EamAssetModelDTO> list = eamAssetModelService.getList(requestParam);
        ExcelUtils.exportExcelToTarget(response, null, list, EamAssetModelExcel.class);
    }

    /**
     * 导入资产型号
     */
    @PostMapping("upload")
    public RetResult<?> upload(MultipartFile file) throws IOException {
        // 读取excel
        List<EamAssetModelUploadExcel> items = EasyExcel.read(file.getInputStream(),EamAssetModelUploadExcel.class,null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
        insertBatch(items);
        return RetResult.ok();
    }

    public void insertBatch(List<EamAssetModelUploadExcel> items){
        eamAssetModelService.insertBatch(items);
    }

    /**
     * 下载模板
     */
    @PostMapping("exportTemp")
    @ApiOperation("下载模板")
    public void exportTemp(HttpServletResponse response) throws Exception {
        List<EamAssetModelUploadExcel> list = new ArrayList<>();
        list.add(new EamAssetModelUploadExcel());
        ExcelUtils.exportExcelToTarget(response, null, list, EamAssetModelUploadExcel.class);
    }


    /**
     *  删除资产型号
     */
    @ApiOperation("删除资产型号")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "资产型号管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamAssetModelService.deleteitem(id);
        return RetResult.ok();
    }
}
